package com.zx.framework.web.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;
/**
 * 用于统计在线人数
 * @author stylewolf
 *
 */
@WebListener
public class SessionCountListener extends HttpServlet implements
		HttpSessionListener {
	private static final Logger LOGGER = Logger
			.getLogger(SessionCountListener.class);
	 private static int sessionCounter = 0;  
	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		 sessionCounter++;  
		 LOGGER.info("session created:" + sessionCounter);  
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		if(sessionCounter>0){
			sessionCounter--;  
		}
		LOGGER.info("session created:" + sessionCounter);  
	}
	
	public static int getSessionCounter() {
		return sessionCounter;
	}
}
